var currCommodity = {};
var comId = $("input[name=id]").val();
if (comId && comId !== '') {
    $.get("pointsystem/commoditygetById/" + comId, function (com) {
        if (com) {
            $("#cName").val(com.cname);
            $("#cName").attr("disabled", true);
            if(com.type === "REDPACKET"){
               $(".redmoney").css("display","block"); 
            }
            $("#redmoney").val(com.money);
            $("#redmoney").attr("disabled", true);
            $("#unit").val(com.unit);
            $("#description").val(com.description);
            $("#points").val(com.points);
            $("#points").attr("disabled", true);
            $("#rewardMethod").multiselect('select', com.type);
            $("#rewardMethod").multiselect('refresh');
            $("#commodityCategory").multiselect('select', com.category);
            $("#commodityCategory").multiselect('refresh');
            $('button.multiselect:first').prop("disabled", true);
            currCommodity = com;
        }
    });
} else {
    $("#createUserMode").removeClass("hidden");
}
$('.multiselect').multiselect();
/**
 * 初始化表单验证
 * @param {type} param
 */
$("#commodityForm").validate({
    //错误类名和元素标签类型
    errorClass: "gg-error",
    validClass: "gg-success",
    errorElement: "label",
    //验证规则正则表达式
    rules: {
        cName: {
            required: true,
            //regx: /(^[\u4e00-\u9fa5]{2,10}$)|(^\s*$)/
        },
        redmoney: {
            regx: /^\d{1,6}$/
        },
        commodity_type: {
            required: true
        },
        commodity_category: {
            required: true
        },
        description: {
            required: true
        },
        unit: {
            required: true,
            regx: /[\u4e00-\u9fa5]{1}$/
        },
        points: {
            required: true,
            regx: /^\d{1,6}$/
        }
    },
    //验证结果文字提示
    messages: {
        cName: {
            required: '必填',
            //regx: '格式不正确',
        },
        redmoney: {
            regx: '格式不正确必须为数字'
        },
        commodity_type: {
            required: '类型没有选择',
        },
        commodity_category: {
            required: '类型没有选择',
        },
        description: {
            required: '必填'
        },
        unit: {
            required: '必填',
            regx: '格式不正确必须为汉字'
        },
        points: {
            required: '必填',
            regx: '格式不正确必须为数字',
            remote: '已存在'
        }
    },
    //验证结果处理
    highlight: function (element, errorClass, validClass) {
        $(element).closest('.form-group').addClass(errorClass).removeClass(validClass);
    },
    unhighlight: function (element, errorClass, validClass) {
        $(element).closest('.form-group').removeClass(errorClass).addClass(validClass);
    },
    //错误信息位置
    errorPlacement: function (error, element) {
        error.insertAfter(element);
    }
});

/**
 * 提交表单
 * 
 * @param {type} param
 */
$("#saveCommodityBtn").click(function () {
    if ($("#commodityForm").valid()) {
        var data = $("#commodityForm").formToJson();
        $.post("pointsystem/commoditysave", data, function (res) {
            alert(res.message);
            if (res.success) {
                location.href = "pointsystem/commoditylist";
            }
        }).fail(function () {
            alert("网络通信错误");
        });
    }
});
function showredmoney() {
    var cg = $("#rewardMethod").val();
    if (cg === "REDPACKET") {
        $(".redmoney").show();
    }else{
        $(".redmoney").hide();
    }
}

